package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.teacher.dao.HonoraryTitleDao;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.HonoraryTitle;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLHonoraryTitleInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.HonoraryTitleInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddHonoraryTitleMutation implements DataFetcher {

    @Autowired
    HonoraryTitleDao honoraryTitleDao;

    @Autowired
    TInformationDao tInformationDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 添加荣誉称号
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLHonoraryTitleInput input = HonoraryTitleInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        HonoraryTitle honoraryTitle = null;

        if (input.getId() != null) {
            honoraryTitle = honoraryTitleDao.get(HonoraryTitle.class, input.getId());
            honoraryTitle.setUpdatedAt(new Date());
        } else {
            honoraryTitle = new HonoraryTitle();
            honoraryTitle.setCreatedAt(new Date());
        }
        if(schoolId != null){

            honoraryTitle.setSchool_id(schoolId);
        }

        if(input.getTeacher_id() != null){

            honoraryTitle.setTeacher_id(input.getTeacher_id());
        }
        if(input.getImage() != null){

            honoraryTitle.setImage(input.getImage());
        }
        if(input.getShenhe() != null){

            honoraryTitle.setShenhe(input.getShenhe());
        }
        if(input.getLeve() != null){

            honoraryTitle.setLeve(input.getLeve());
        }
        if(input.getTime() != null){

            honoraryTitle.setTime(input.getTime());
        }
        if(input.getName() != null){

            honoraryTitle.setName(JSON.toJSONString(input.getName()));
        }
        if(input.getGrade() != null){

            honoraryTitle.setGrade(JSON.toJSONString(input.getGrade()));
        }
        if(input.getRemark() != null){

            honoraryTitle.setRemark(JSON.toJSONString(input.getRemark()));
        }
        try{
            honoraryTitleDao.saveOrUpdate(honoraryTitle);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

